# -*- coding: utf-8 -*-
"""userSetup.py by Erkan Ozgur Yilmaz (c) 2010

v11.4.4

Description
===========

The setup script for maya environment. Forces the user to set a valid project
and sequence from the interfacde

Version History
===============

v11.4.4
-------

 * Removed the unnecessary PATH environment key exports

 * Merged maya 2011 and 2009 versions of this script

v11.3.15
--------

 * moved some code outside of the skipUser check
 * added path variables for linux and osx

v11.3.12
--------

 * migrated userSetup.mel to this script completely

v10.12.29
---------

 * added auto plugin load for Mayatomr and objExport

v10.5.17
--------

 * modifications for Maya 2011 and Pymel 1.0.2

v10.2.6
-------

 * pm.isStandalone is not working under windows, switched to cmds.about(b=True)

v10.2.5
-------

 * Finally moved to python

"""

import sys
import os
import platform

import pymel.core as pm
import maya.cmds as cmds



__version__ = "11.3.15"



# ----------------------------------------------------------------------------
# skip user setup if there is any _skip_userSetup_ file in paths
skipUserSetup = False
skipUserSetupHintFileName = '_skip_userSetup_'

for path in sys.path:
    if os.path.exists (
        os.path.expanduser(
            os.path.join(path, skipUserSetupHintFileName)
        )
     ):
        skipUserSetup = True
# ----------------------------------------------------------------------------



# ----------------------------------------------------------------------------
# skip user setup if maya is in batch mode
# pm.isStandalone is not working under windows so use cmds.about(b=True)
if pm.general.about(b=1) or cmds.about(b=True):
    skipUserSetup = True
# ----------------------------------------------------------------------------


# ----------------------------------------------------------------------------
if not skipUserSetup:
    from oyProjectManager.ui import projectSetter
    projectSetter.setProject('MAYA', True)
else:
    print "skipping python user setup!!!"
# ----------------------------------------------------------------------------



# ----------------------------------------------------------------------------
# in any condition run this lines
# code migrated from userSetup.mel
try:
    pm.Mel.source("InitDeadlineSubmitter.mel")
except pm.MelError:
    pass

try:
    pm.Mel.eval("toggleMenuBarsInAllPanels 1")
except pm.MelError:
    pass

# try:
#     pm.Mel.eval("madRecentCrashFile()")
# except pm.MelError:
#     pass

# try:
#     pm.Mel.source("HKLocalTools")
# except pm.MelError:
#     pass
# ----------------------------------------------------------------------------



# ----------------------------------------------------------------------------
def __pluginLoader(pluginName):
    if not pm.pluginInfo(pluginName, q=1, loaded=1):
        pm.loadPlugin(pluginName)

__pluginLoader('Mayatomr')
__pluginLoader('objExport')


# set the optionVar that enables hidden mentalray shaders
pm.optionVar['MIP_SHD_EXPOSE'] = 1
pm.runtime.SavePreferences()
# ----------------------------------------------------------------------------


# export the environment variable
from oyProjectManager.models import repository
repo = repository.Repository()
os.environ[repo.repository_path_env_key] = repo.server_path
